package com.bruce.pattern.behavioral.chain.demo2;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by bruce on 2018/11/11 18:25
 */
@Slf4j
public class Chain<T> {

    private ChainHandler<T> firstHandler;
    private ChainHandler<T> nextHandler;

    public Chain<T> nextHandler(ChainHandler<T> handler) {
        if (firstHandler == null) {
            this.firstHandler = handler;
            this.nextHandler = handler;
        } else {
            this.nextHandler = this.nextHandler.nextHandler(handler);
        }
        return this;
    }

    public void execute(T obj) {
        if (firstHandler != null) {
            firstHandler.execute(obj);
        }else {
            log.warn("责任链上没有任何处理器");
        }
    }


}
